package com.hcss.core.common.logsmgt.dao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.hcss.core.common.logsmgt.model.OpertionLog;
import com.hcss.core.mvc.BaseDao;
import com.hcss.core.mvc.page.Pager;


/** 
 * @ClassName: OpertionDao 
 * @Description: TODO 行为审核DAO
 * @author lvfang
 * @date 2013-12-9 下午4:40:56 
 *  
 */
@Repository("opertionDao")
public class OpertionDao  extends BaseDao{
	private static final String TBLPREFIX = "t_opLog";
		
	public static String table() {
	       return TBLPREFIX;
	}
	
	/** 
	* <p> addOpLog TODO增加行为日志</p>
	* @param log
	* @return  int 设定文件 
	*/
	public int addOpLog(final OpertionLog log){
		if (log == null) {
			return 0;
		}
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO ");
		sql.append(table());
		sql.append(" (opModel, opFunction, opUserId, opIp,opTime,opMark) VALUES (?, ?, ?, ?, ?, ?)");
		Object[] params = new Object[]{
			log.getOpModel(),
			log.getOpFunction(),
			log.getOpUserId(),
			log.getOpIp(),
			log.getOpTime(),
			log.getOpMark()
		};
		return jdbcTemplate.update(sql.toString(), params);
	}
	
	/**
	 * 返回所有行为日志
	 * @return
	 * @throws Exception
	 */
	public Pager getAllOpertion(Pager page,String userName,String opModel,String opFunction,String opIp)throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select o.*,u.userName from T_opLog o ");
		sql.append(" left join t_userinfo u on o.opUserId = u.id");
		sql.append(" where u.isDeleted = 'n' ");
		if(userName != null && !"".equals(userName)){
        	sql.append(" and u.userName = '"+userName +"' ");
        }
		if(opModel != null && !"".equals(opModel)){
			sql.append(" and o.opModel like '%"+opModel+"%' ");
		}
		if(opFunction != null && !"".equals(opFunction)){
			sql.append(" and o.opFunction like '%"+opFunction+"%' ");
		}
		if(opIp != null && !"".equals(opIp)){
			sql.append(" and o.opIp like '%"+opIp+"%' ");
		}
		RowMapper<?> rowMapper = new BeanPropertyRowMapper<OpertionLog>(OpertionLog.class);
		return this.getPageList(sql.toString(), page, rowMapper);
	}
}
